<template>
  <el-card>
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="toShowSpuForm"
      >添加SPU</el-button
    >
    <el-table border :data="spuList" v-loading="loading" style="margin: 10px 0">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="80"
      ></el-table-column>
      <el-table-column label="SPU名称" prop="spuName"></el-table-column>
      <el-table-column label="SPU描述" prop="description"></el-table-column>
      <el-table-column label="SPU操作">
        <template v-slot="{ row, $index }">
          <el-tooltip
            class="box-item"
            effect="dark"
            content="添加SKU"
            placement="top"
          >
            <el-button
              type="primary"
              size="small"
              :icon="Plus"
              @click="toAddSkuInfo(row)"
            />>
          </el-tooltip>
          <el-tooltip
            class="box-item"
            effect="dark"
            content="编辑SPU"
            placement="top"
          >
            <el-button
              type="primary"
              size="small"
              :icon="Edit"
              @click="toEditSpuInfo(row)"
            />
          </el-tooltip>
          <el-tooltip
            class="box-item"
            effect="dark"
            content="查看SKU"
            placement="top"
          >
            <el-button
              type="info"
              size="small"
              :icon="InfoFilled"
              @click="toCheckSkuList(row)"
            />
          </el-tooltip>

          <el-popconfirm
            confirm-button-text="Yes"
            cancel-button-text="No"
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="Are you sure to delete this?"
            @confirm="toDeleteSpuInfo(row, $index)"
          >
            <template #reference>
              <el-button type="danger" size="small" :icon="Delete" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout=" prev, pager, next, jumper, ->,sizes,total"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 弹出来的对话框 -->
    <el-dialog v-model="skuListTableVisible" title="Sku列表数据">
      <el-table :data="skuList">
        <el-table-column property="skuName" label="名称" width="150" />
        <el-table-column property="price" label="价格(元)" width="200" />
        <el-table-column property="weight" label="重量(KG)" />
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <el-image :src="row.skuDefaultImg" style="width: 100px"></el-image>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

  </el-card>
</template>

<script lang="ts">
export default {
  name: 'SpuList',
};
</script>
<script lang="ts" setup>
import { ref, watch } from 'vue';
import { Plus, Edit, Delete, InfoFilled } from '@element-plus/icons-vue';
import { reqDeleteSpuInfo, reqGetSpuListData } from '@/api/product/spu';
import { useCategoryListStore } from '@/stores/categoryList';
import { ShowStatus } from '../types';
import { reqDeleteAttrInfo } from '@/api/product/attr';
import { ElMessage } from 'element-plus';
import { reqCheckSkuListData } from '@/api/product/sku';

const categoryStore = useCategoryListStore();
const spuList = ref([]);
const skuList = ref([]);
const currentPage = ref(1);

const skuListTableVisible = ref(false);
const pageSize = ref(3);
const total = ref(0);
const small = ref(false);
const loading = ref(false);
const background = ref(false);
const disabled = ref(false);

const emits = defineEmits(['setShowStatus', 'getSpuInfo']);

// 1. 定义一个获取spuList列表数据的函数
const getSpuListData = async () => {
  loading.value = true;
  const result = await reqGetSpuListData(
    currentPage.value,
    pageSize.value,
    categoryStore.category3Id
  );

  // 赋值
  loading.value = false;
  spuList.value = result.records;
  total.value = result.total;
};

// 2. 监听三级导航id变化 然后获取spu列表数据
watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) return (spuList.value = []);

    // 重新发送请求渲染列表
    getSpuListData();
  },
  { immediate: true }
);

// 3. 单击页码实现分页效果
const handleCurrentChange = (val: number) => {
  currentPage.value = val;

  // 重新发送请求
  getSpuListData();
};

// 4. 修改每页显示的数量
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  currentPage.value = 1; // 默认跳转到第1页

  getSpuListData();
};

// 5. 去跳转到添加spuForm的页面 也就是更改status的值
const toShowSpuForm = () => {
  // 去添加新的SpuInfo的时候 要将原来的内容全部清除掉
  emits('getSpuInfo', {});
  emits('setShowStatus', ShowStatus.SPU_FORM);
};

// 6. 删除spuInfo数据
const toDeleteSpuInfo = async (row, index) => {
  await reqDeleteSpuInfo(row.id);

  ElMessage.success('spu对象删除成功了!!!');

  // 为了节约服务器资源 减少一次请求的发送
  spuList.value.splice(index, 1);
};

// 7. 编辑SpuInfo 之数据回回显
const toEditSpuInfo = (row) => {
  // 1 传递待编辑的spuInfo数据
  emits('getSpuInfo', row);

  // 2 还要进行页面切换
  emits('setShowStatus', ShowStatus.SPU_FORM);
};

// 8. 去添加skuInfo数据 需要传递当前的spuInfo对象过去
const toAddSkuInfo = (row) => {
  // 1 传递待编辑的spuInfo数据
  emits('getSpuInfo', row);

  // 2 还要进行页面切换
  emits('setShowStatus', ShowStatus.SKU_FORM);
};

// 9. 查看skuList列表  是spu下面的skuList列表
const toCheckSkuList = async row => {
  const result = await reqCheckSkuListData(row.id);
  skuListTableVisible.value = true;
  skuList.value = result;
};
</script>

<style></style>
